package com.dragon.modules.yuegou.service.impl.system;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.system.UniappOpenidRefEntity;
import com.dragon.modules.yuegou.mapper.system.UniappOpenidRefMapper;
import com.dragon.modules.yuegou.model.system.UniappOpenidRef;
import com.dragon.modules.yuegou.query.system.UniappOpenidRefQuery;
import com.dragon.modules.yuegou.service.system.UniappOpenidRefService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * uniapp openid 手机号 关系表 服务实现
 *
 * @author pbuilder generated
 * @date 2022-11-09 11:09:28
 * @version 2.0
 */
@Service("sysUniappOpenidRefService")
public class UniappOpenidRefServiceImpl
		extends ServiceImpl<UniappOpenidRefMapper, UniappOpenidRefEntity>
		implements UniappOpenidRefService {


	@Override
	public void saveUniappOpenidRef(UniappOpenidRef uniappOpenidRef) {
		//判断是否存在
		UniappOpenidRefQuery query = new UniappOpenidRefQuery();
		query.setAppid(uniappOpenidRef.getAppid());
		query.setUniappOpenId(uniappOpenidRef.getUniappOpenId());
		List<UniappOpenidRefEntity> uniappOpenidRefList = baseMapper.selectList(query.toQueryWrapper());
		if (CollectionUtils.isNotEmpty(uniappOpenidRefList)){
			UniappOpenidRefEntity openidRef = uniappOpenidRefList.get(0);
			openidRef.setMobile(uniappOpenidRef.getMobile());
			baseMapper.updateById(openidRef);
		}else {
			uniappOpenidRef.setCreatedTime(LocalDateTime.now());
			save(uniappOpenidRef);
		}
	}

	@Override
	public UniappOpenidRef selectByAppIdAndOpenId(String uniAppId, String openId) {
		UniappOpenidRefQuery query = new UniappOpenidRefQuery();
		query.setAppid(uniAppId);
		query.setUniappOpenId(openId);
		List<UniappOpenidRefEntity> uniappOpenidRefList = list(query.toQueryWrapper());
		if (CollectionUtils.isNotEmpty(uniappOpenidRefList)){
			UniappOpenidRefEntity openidRef = uniappOpenidRefList.get(0);
			return new UniappOpenidRef(openidRef);
		}
		return null;
	}
}